import time
from configurations.cpp import cp
from loggingconf.log import logger
class TimeController():

    def __init__(self):
        self.timeStep = cp.getint("time","timestep")
        self.task = cp.get("task","task").strip()
        pass

    def currenttime_s(self):
        t = time.time()
        return int(t)

    def currenttime_ms(self):
        t = time.time()
        return int(round(t * 1000))

    def isInRecord(self, timestamp):
        """
        :param timestamp:
        :return:
        """
        time_local = time.localtime(timestamp)
        dt = time.strftime("%Y-%m-%d_%H:%M:%S",time_local)
        logger.debug(dt)
        tail = dt.split(":")[-1].strip()
        head = dt[:-3].strip()
        tail = int(tail)

        if self.task == "steal":
            if( (tail >= 15 and tail < 20) ):
                return True,head+"_a"
            elif (tail >= 35 and tail < 40):
                return True,head+"_b"
            elif tail >= 55:
                return True,head+"_c"
            return False,""
        else:
            a = tail / 10
            b = tail % 10
            if( b >= 5):
                return True,head+"_%02d"%a
            else:
                return False,""

    def isInRecord2(self):
        timestamp_ms = self.currenttime_ms()
        timestamp = self.currenttime_s()
        ret,subfolder = self.isInRecord(timestamp)
        if ret:
            return True,timestamp_ms,subfolder
        else:
            return False,timestamp_ms,""


tc = TimeController()